package com.chaos.auth.controller;

import com.chaos.auth.api.data.RbacRoleMenuBtnData;
import com.chaos.auth.api.dto.auth.RbacRoleMenuBtnDto;
import com.chaos.auth.entity.RbacPermissionMenuBtn;
import com.chaos.auth.entity.RbacRoleMenuBtn;
import com.chaos.auth.service.RbacRoleMenuBtnService;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.data.BooleanResult;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.starter.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@RestController
@Api(tags = "角色菜单按钮关联表")
@RequestMapping("/rbacRoleMenuBtn")
public class RbacRoleMenuBtnController extends BaseController<RbacRoleMenuBtnData> {
    @Resource
    private RbacRoleMenuBtnService rbacRoleMenuBtnService;

    @PostMapping("/one.admin")
    @ApiOperation(value = "角色菜单按钮关联表详情", httpMethod = "POST")
    public DataResult<RbacRoleMenuBtnData> one(@RequestBody ID data) {
        return dataResult(rbacRoleMenuBtnService.selectByID(data));
    }

    @PostMapping("/list.admin")
    @ApiOperation(value = "角色菜单按钮关联表列表", httpMethod = "POST")
    public ListResult<RbacRoleMenuBtnData> list(@RequestBody RbacRoleMenuBtnData data) {
        return listResult(rbacRoleMenuBtnService.selectByData(data));
    }

    @PostMapping("/page.admin")
    @ApiOperation(value = "角色菜单按钮关联表分页", httpMethod = "POST")
    public PageResult<RbacRoleMenuBtnData> page(@RequestBody PageQueryDto<RbacRoleMenuBtnData> data) {
        return pageResult(rbacRoleMenuBtnService.selectByPage(data));
    }

    @PostMapping("/add.admin")
    @ApiOperation(value = "新增角色菜单按钮关联表", httpMethod = "POST")
    public BooleanResult add(@RequestBody RbacRoleMenuBtnDto data) {
        List<RbacRoleMenuBtn> list = data.getMenuOrBtnIds().stream().map(
                        menuOrBtnId -> RbacRoleMenuBtn
                                .builder()
                                .menuOrBtnId(menuOrBtnId)
                                .roleId(data.getRoleId())
                                .correlationType(data.getCorrelationType())
                                .build())
                .collect(Collectors.toList());
        return booleanResult(rbacRoleMenuBtnService.saveBatch(list));
    }

    @PostMapping("/update.admin")
    @ApiOperation(value = "编辑角色菜单按钮关联表", httpMethod = "POST")
    public BooleanResult update(@RequestBody RbacRoleMenuBtnData data) {
        return booleanResult(rbacRoleMenuBtnService.updateModelByID(data));
    }

    @PostMapping("/delete.admin")
    @ApiOperation(value = "删除角色菜单按钮关联表", httpMethod = "POST")
    public BooleanResult delete(@RequestBody ID data) {
        return booleanResult(rbacRoleMenuBtnService.deleteModel(data));
    }

}
